<template>
  <div class="container">
    <topSearch :form='form' :showAdd="checkPermission(['articleCreate'])" @addItem='addItem' @searchlist='searchlist' @resetform='resetform'></topSearch>

    <el-row>
      <el-table :data="tableData" border style="width: 100%; margin-top: 20px;" size="small">
        <el-table-column
          prop="id"
          label="序号"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="title"
          label="标题"
          show-overflow-tooltip
        ></el-table-column>
          <el-table-column
          prop="category"
          label="分类"
          show-overflow-tooltip
        ></el-table-column>
          <el-table-column
          prop="hits"
          label="点击数"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column prop="picture" label="封面图" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-image
              style="width: 50px; height: 50px"
              :src="scope.row.picture[0]"
              :preview-src-list="scope.row.picture"
            >
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-switch
              class="switch"
              v-if="checkPermission(['articleReStatus'])"
              v-model="scope.row.status"
              @change="changestatus(scope.row)"
              :active-value="1"
              :inactive-value="0"
              active-text="发布"
              inactive-text="草稿"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column
          prop="create_time"
          label="创建时间"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="update_time"
          label="更新时间"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column prop="date" label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="editItem(scope.row)"
              v-if="checkPermission(['articleUpdate'])"
              >编辑</el-button
            >
            <el-button
              type="text"
              @click="deleteTableItem(scope.row)"
              v-if="checkPermission(['articleDelete'])"
              >删除
            </el-button>
            <el-button
              type="text"
              @click="refreshTab(scope.row)"
              v-if="checkPermission(['articleRefresh'])"
              >刷新
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row>
        <el-col :span="24" class="pagination">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :page-sizes="[10, 20]"
            :page-size="10"
            background
            :current-page.sync="page"
            layout="total, sizes, prev, pager, next"
            :total="pagination.total"
          >
          </el-pagination>
        </el-col>
      </el-row>
    </el-row>

    <!-- 弹框 -->
    <subdialog
      :isshowDialog="isshowDialog"
      :dialogTitle="dialogTitle"
      :formType="formtype"
      @confirm="confirm"
      @closedialog="closedialog"
      :dialogdata="dialogdata"
      :option='option'
    ></subdialog>
  </div>
</template>

<script type="text/javascript">
import checkPermission from "@/utils/permission"; // 权限判断函数
import {
  article,
  articleRestatus,
  articleDelete,
  articleRead,
  articleRefresh,
} from "@/api/information";
import subdialog from "./components/dialog";
import topSearch from "@/components/topSearch";
export default {
  data() {
    return {
      isshowDialog: false,
      dialogTitle: "新增",
      formtype: 0,
      page: 1,
      rows: 10,
      form: {
        state: null,
      },
      tableData: [],
      category: [],
      option: [],
      pagination: {},
      dialogdata: null,
    };
  },
  components: {
    subdialog,
    topSearch
  },
  created() {
    this.getData();
  },
  methods: {
    checkPermission,
    handleSizeChange(val) {
      this.rows = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getData();
    },
    searchlist() {
       this.page= 1,

      this.getData();
    },
    resetform() {
    
      this.getData();
    },
    //详情
    async getquestionRead(id) {
      let data = await articleRead({
        id: id,
      });
      this.dialogdata = data.data;
    },
    //切换类型
    async changestatus(val) {
      let status = val.status == 0 ? 1 : 0;
      let params = {
        id: val.id,
        status: status,
      };
      let data = await articleRestatus(params);
      if (data.code == "0000") {
        this.$message({
          type: "success",
          message: data.msg,
        });
        this.getData();
      }
    },
    //新增
    addItem() {
      this.isshowDialog = true;
      this.dialogTitle = "新增";
      this.formtype = 0;
    },
    //编辑
    editItem(val) {
      
      this.isshowDialog = true;
      this.dialogTitle = "编辑";
      this.formtype = 1;
      this.getquestionRead(val.id);
    },
    //刷新
    async refreshTab(val) {
      let data = await articleRefresh({
        id: val.id,
      });
      if (data.code == "0000") {
        this.getData();
      }
    },
    //删除
    async deleteTableItem(val) {
      let data = await articleDelete({
        id: val.id,
      });
      if (data.code == "0000") {
        this.$message({
          type: "success",
          message: "删除成功!",
        });
        this.getData();
      }
    },
    //提交成功
    confirm() {
      this.isshowDialog = false;
      this.getData();
    },
    closedialog() {
      this.isshowDialog = false;
    },
    //获取列表
    async getData() {
      let params = {
        page: this.page,
        rows: this.rows,
      };
      if (this.form.keyWord) {
        params.keyword = this.form.keyWord;
      }

      let data = await article(params);
      console.log(data);
      let datainfo = data.data;
      this.tableData = datainfo.list;
      this.option = datainfo.option;
      this.pagination = datainfo.page;
    },
  },
  mounted() {},
};
</script>

<style lang="scss" scoped>

</style>
